﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCDemo.Entity.POCOModel;
using MVCDemo.Entity.ViewModel;


namespace MVCMult.Controllers
{
    public class MemberController : Controller
    {
        // GET: Member
        public ActionResult Index()
        {
            MembersModel db = new MembersModel();
            var viewmodal = db.Shops.ToList().Select(u => new ShopViewModel()
            {
                S_Address = u.S_Address,
                S_Category = u.S_Category,
                S_ContactName = u.S_ContactName,
                S_ContactTel = u.S_ContactTel,
                S_ID = u.S_ID,
                S_Name = u.S_Name,
                S_Remark = u.S_Remark
            }).ToList();
            return View(viewmodal);
        }

        // GET: Member/Details/5
        public ActionResult Details(int id)
        {
            MembersModel db = new MembersModel();
            var smodel = db.Shops.FirstOrDefault(u => u.S_ID == id);
            ShopViewModel smodels = new ShopViewModel();
            smodels.S_Address = smodel.S_Address;
            smodels.S_Category = smodel.S_Category;
            smodels.S_ContactName = smodel.S_ContactName;
            smodels.S_ContactTel = smodel.S_ContactTel;
            smodels.S_ID = smodel.S_ID;
            smodels.S_Name = smodel.S_Name;
            smodels.S_Remark = smodel.S_Remark;
            return View(smodels);
        }

        // GET: Member/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Member/Create
        [HttpPost]
        public ActionResult Create(ShopEditViewModel smodels)
        {
            try
            {
                // TODO: Add insert logic here
                if (ModelState.IsValid)
                {
                    MembersModel db = new MembersModel();
                    Shops smodel = new Shops();
                    smodel.S_Address = smodels.S_Address;
                    smodel.S_Category = smodels.S_Category;
                    smodel.S_ContactName = smodels.S_ContactName;
                    smodel.S_ContactTel = smodels.S_ContactTel;
                    smodel.S_CreateTime = smodels.S_CreateTime;
                    smodel.S_IsHasSetAdmin = smodels.S_IsHasSetAdmin;
                    smodel.S_Name = smodels.S_Name;
                    smodel.S_Remark = smodels.S_Remark;
                    db.Shops.Add(smodel);
                    if (db.SaveChanges() > 0)
                    {
                        return RedirectToAction("Index");
                    }
                    else
                    {
                        return View();
                    }
                }
                else
                {
                    ModelState.AddModelError("", "输入内容格式不正确");
                    return View();
                }

            }
            catch
            {
                return View();
            }
        }

        // GET: Member/Edit/5
        public ActionResult Edit(int id)
        {
            MembersModel db = new MembersModel();
            var smodel = db.Shops.FirstOrDefault(u => u.S_ID == id);
            ShopEditViewModel smodels = new ShopEditViewModel()
            {
                S_Address = smodel.S_Address,
                S_Category = smodel.S_Category,
                S_ContactName = smodel.S_ContactName,
                S_ID = smodel.S_ID,
                S_ContactTel = smodel.S_ContactTel,
                S_CreateTime = smodel.S_CreateTime,
                S_IsHasSetAdmin = smodel.S_IsHasSetAdmin,
                S_Name = smodel.S_Name,
                S_Remark = smodel.S_Remark
            };

            return View(smodels);
        }

        // POST: Member/Edit/5
        [HttpPost]
        public ActionResult Edit(ShopEditViewModel smodel)
        {
            try
            {
                // TODO: Add update logic here
                if (ModelState.IsValid)
                {
                    MembersModel db = new MembersModel();
                    var smodels = db.Shops.FirstOrDefault(u => u.S_ID == smodel.S_ID);
                    smodels.S_Address = smodel.S_Address;
                    smodels.S_Category = smodel.S_Category;
                    smodels.S_ContactName = smodel.S_ContactName;
                    smodels.S_ID = smodel.S_ID;
                    smodels.S_ContactTel = smodel.S_ContactTel;
                    smodels.S_CreateTime = smodel.S_CreateTime;
                    smodels.S_IsHasSetAdmin = smodel.S_IsHasSetAdmin;
                    smodels.S_Name = smodel.S_Name;
                    smodels.S_Remark = smodel.S_Remark;
                    if (db.SaveChanges() > 0)
                    {
                        return RedirectToAction("Index");
                    }
                    else
                    {
                        return View();
                    }
                }
                else
                {
                    ModelState.AddModelError("","输入格式不正确");
                    return View();
                }
            }
            catch
            {
                return View();
            }
        }

        // GET: Member/Delete/5
        public ActionResult Delete(int id)
        {
            MembersModel db = new MembersModel();
            var smodel = db.Shops.FirstOrDefault(u => u.S_ID == id);
            return View(smodel);
        }

        // POST: Member/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                MembersModel db = new MembersModel();
                var smodel = db.Shops.FirstOrDefault(u => u.S_ID == id);
                db.Shops.Remove(smodel);
                if (db.SaveChanges() > 0)
                {
                    return RedirectToAction("Index");
                }
                else
                {
                    return View();
                }
            }
            catch
            {
                return View();
            }
        }
    }
}
